﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using Web.Utils;

namespace Web.Page
{
    public partial class WLKC_Query : BasePage
    {
        protected KC_Dao dao = new KC_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            InitWL();

            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            this.ddlGYS.DataSource = daoGYS.QueryAll();
            this.ddlGYS.DataValueField = "ID";
            this.ddlGYS.DataTextField = "MC";
            this.ddlGYS.DataBind();
            this.ddlGYS.Items.Insert(0, new ListItem("请选择", "0"));

            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();
            this.ddlCWID.DataSource = lsCW;
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();
            this.ddlCKID.DataSource = lsCK;
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        private void InitWL()
        {
            this.ddlWLLX.Items.Clear();
            CS_WLLX_Dao daoWL = new CS_WLLX_Dao();
            List<CS_WLLX> lsZP = daoWL.QueryAll();
            this.ddlWLLX.DataSource = lsZP;
            this.ddlWLLX.DataValueField = "BH";
            this.ddlWLLX.DataTextField = "MC";
            this.ddlWLLX.DataBind();
            this.ddlWLLX.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();

            this.GridView1.DataSource = query();
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((LinkButton)(e.Row.Cells[8].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/WLKC_Add.aspx?op=01");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected List<vWLKC> query()
        {
            string lx = "01";
            string wpid = this.ddlWLLX.SelectedValue;
            string ckid = this.ddlCKID.SelectedValue;
            string cwid = this.ddlCWID.SelectedValue;
            string mc = this.txtMC.Text.Trim();

            return dao.QueryWLBySql(lx, wpid, this.ddlGYS.SelectedValue, this.txtMC.Text.Trim(), ckid, cwid);
        }
    }
}